import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './assets/css/global.css'
import router from './router'

import { useAuthStore } from './stores/auth'
import { useWebSocketStore } from './stores/websocket'


const pinia = createPinia()
pinia.use(({ store }) => {
    store.$router = router
})

const app = createApp(App).use(pinia).use(router).use(ElementPlus)

router.isReady().then(() => {
    const authStore = useAuthStore()
    authStore.initAuthFromStorage()

    setTimeout(() => {
        const wsStore = useWebSocketStore()
        wsStore.initWebSocket(router.currentRoute.value?.path || '')
    }, 300)

    app.mount('#app')
}).catch(err => {
    console.error('路由初始化失败，不影响应用启动:', err)
    const authStore = useAuthStore()
    authStore.initAuthFromStorage()
    app.mount('#app')
})
